﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using ApimDelExample.Properties;

namespace ApimDelExample.Controllers
{
    public class ApimDelegationController : Controller
    {
        // GET: ApimDelegation
        public async Task<ActionResult> Index(string operation, string returnUrl, string salt, string sig, string userId, string productId, string subscriptionId)
        {
            //            if (!)
            //                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request");
            switch (operation.ToLower())
            {
                case "signin":
                    var check = APIMHandler.CheckHash(returnUrl + '\n' + salt, sig, Settings.Default.ApimDelegationSecret);
                    return RedirectToAction("Login", "Account", new { returnUrl = Settings.Default.ApimPortal + returnUrl });
                case "signup":
                    return RedirectToAction("Register", "Account");
                case "subscribe":
                    //normally this would be the other way round - get cash then subscribe...
                    if (await APIMHandler.SubscribeToProduct(Guid.NewGuid().ToString(), userId, productId))
                        return RedirectToAction("index", "Billing", new { returnUrl = Settings.Default.ApimPortal + returnUrl });
                    break;
                case "unsubscribe":
                    if (await APIMHandler.UpdateSubscriptionState(subscriptionId, "cancelled"))
                        return Redirect(Settings.Default.ApimPortal);
                    break;
                case "renew":
                    break;
            }
            ViewBag.Operation = operation;
            return View();
        }

    }
}